package xiyuan.view.distance;

import java.net.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.*;

public class GoogleDistance {
    public GoogleDistance() {
    }
    
    public static double getDistance(String a, String b){
    	int result1 = 0;
		int result2 = 0;
		int result3 = 0;
		try {
			result1 = getResultNumber(a);
			result2 = getResultNumber(b);
			result3 = getResultNumber(a + " & " + b);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	double M = Math.log(48000000000L);
    	
    	if (result1 < result2){
    		int temp = result1;
    		result1 = result2;
    		result2 = temp;
    	}	//result1始终>=result2
    	//System.out.println(result1 + " " + result2 + " " + result3 + " " + M);
    	//����Ҫ����Nֵ��С
    	double NGD = (Math.log(result1) - Math.log(result3)) / (M - Math.log(result2));
    	//System.out.println("NGD RESULT = " + NGD);
    	return NGD;
    	
    }
    
  //以下为get.java里面的getresult方法
	 public static int getResultNumber(String key) throws IOException {
        String countStr = null;
            key = URLEncoder.encode(key, "UTF-8");
            URL u = new URL("http://www.baidu.com.cn/s?wd=" + key + "&cl=3");
            URLConnection conn = u.openConnection();
            BufferedReader reader;
            try{
	            reader = new BufferedReader(new InputStreamReader(
	                    conn.getInputStream(), "gb2312"));
            }catch(SocketException e){
	            reader = new BufferedReader(new InputStreamReader(
	                    conn.getInputStream(), "UTF-8"));
            }
            String str = reader.readLine();
            String text = "";
            while (str != null) {
           	 	text += str;
	            if (str.contains("charset=")){
	            	if (str.contains("utf-8"))
	            	reader = new BufferedReader(new InputStreamReader(
	                        conn.getInputStream(), "UTF-8"));
	            	break;
	            }
	           	 str = reader.readLine();
            }
            while (str !=null){
            	if (str.contains("<title>")){
            		if (str.contains("百度--您的访问出错了")){
            			try {
							Thread.sleep(1000);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
            			return getResultNumber(key);
            		}
            		break;
            	}
	           	 str = reader.readLine();
            }
            while (str != null) {
            	 str = reader.readLine();
            	 text += str;
                 int index; 
                 if ((str!=null) && (index = str.indexOf("<p id=\"page\">"))>=0) {
                 	str = str.substring(index);
                 	Pattern p = Pattern.compile(".*<span class=\"nums\"[^>]*>找到相关结果约{0,1}([\\d|,]*)个</span>.*");
                 	//Pattern p = Pattern.compile("s(.*)n");
                 	try {
						Matcher m = p.matcher(str);
						m.matches();
						m.groupCount();
						countStr = m.group(1);
					} catch (IllegalStateException e) {
						// TODO Auto-generated catch block
						System.err.println(str);
						e.printStackTrace();
					}
 	               	break;
                 }
            }
            reader.close();
            if (countStr==null){
            	System.out.println("error, no countStr");
            	return -1;
            }
            countStr = countStr.replaceAll(",", "");
        return Integer.parseInt(countStr);
    }
}